package com.thb.pay.channel.alipay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import lombok.Data;
import org.springframework.util.Assert;

@Data
public class AliPaymentConfig {
    /**
     * RSA2
     */
    public static String SIGNTYPE = "RSA2";
    /**
     * 编码
     */
    public static String CHARSET = "UTF-8";
    /**
     * 返回格式
     */
    public static String RETURN_FORMAT = "json";
    /**
     * 合作伙伴身份partner
     */
    private String partnerId;
    /**
     * 应用App ID
     */
    private String appId;
    /**
     * 应用私钥
     */
    private String privateKey;
    /**
     * 支付宝公钥
     */
    private String alipayPublicKey;
    /**
     * 支付宝账号
     */
    private String account;
    /**
     * 请求网关地址
     */
    private String reqUrl;

    public AliPaymentConfig() {
    }

    public AliPaymentConfig(String payParam) {
        Assert.notNull(payParam, "init.alipay.config.error");
        JSONObject object = JSON.parseObject(payParam);
        this.partnerId = object.getString("partnerId");
        this.appId = object.getString("appId");
        this.privateKey = object.getString("privateKey");
        this.alipayPublicKey = object.getString("alipayPublicKey");
        this.account = object.getString("account");
        this.reqUrl = object.getString("reqUrl");
    }

    public AlipayClient buildAlipayClient() {
        AlipayClient client = new DefaultAlipayClient(this.getReqUrl(), this.getAppId(), this.getPrivateKey(), RETURN_FORMAT, CHARSET, this.getAlipayPublicKey(), SIGNTYPE);
        return client;
    }
}

